home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 2078 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.9 KB  |  111 lines

  1. Path: newsfeed.internetmci.com!panix!not-for-mail
  2. From: acinader@panix.com (Arthur Cinader)
  3. Newsgroups: gnu.g++.help,comp.lang.c++
  4. Subject: Operator Overloading - Result not Expected!
  5. Date: 15 Jan 1996 15:32:03 -0500
  6. Organization: Panix
  7. Message-ID: <4dedk3$fo2@panix.com>
  8. NNTP-Posting-Host: panix.com
  9.  
  10. I am teaching myself to program with "How To Program C++" by
  11. Deitel & Deitel.  A great book.  In chapter eight, there is a
  12. an example in the text on operator overloading.  When I code
  13. the example, exactly as it appears in the book, I don't get
  14. the output that they show...
  15.  
  16. Using g++ v2.4 on BSDI I compile with:
  17.  
  18. g++ -o fig8.3 fig8.3.C
  19.  
  20. here is the source:
  21.  
  22. ********begin source
  23. // FIG8_3.CPP
  24. // Overloading the stream-insertion and
  25. // stream-extraction operators.
  26. #include <iostream.h>
  27.  
  28. class PhoneNumber {
  29.    friend ostream &operator<<(ostream &, const PhoneNumber &);
  30.   friend istream &operator>>(istream &, PhoneNumber &);
  31. private:
  32.    char areaCode[4];  // 3-digit area code and null
  33.    char exchange[4];  // 3-digit exchange and null
  34.    char line[5];      // 4-digit line and null
  35. };
  36.  
  37. // Overloaded stream insertion operator (cannot be
  38. // a member function).
  39. ostream &operator<<(ostream &output, const PhoneNumber &num)
  40. {
  41.    output << "(" << num.areaCode << ") "
  42.           << num.exchange << "-" << num.line;
  43.  
  44.    return output;     // enables cout << a << b << c;
  45. }
  46.  
  47. // overloaded stream extraction operator
  48. istream &operator>>(istream &input, PhoneNumber &num)
  49. {
  50.    input.ignore();                 // skip (
  51.    input.getline(num.areaCode, 4); // input area code
  52.    input.ignore(2);                // skip ) and space
  53.    input.getline(num.exchange, 4); // input exchange
  54.    input.ignore();                 // skip dash (-)
  55.    input.getline(num.line, 5);     // input line
  56.  
  57.    return input;      // enables cin >> a >> b >> c;
  58. }
  59.  
  60. main()
  61. {
  62.    PhoneNumber phone; // create object phone
  63.  
  64.    cout << "Enter a phone number in the "
  65.         << "form (123) 456-7890:" << endl;
  66.  
  67.    // cin >> phone invokes operator>> function by
  68.    // issuing the call operator>>(cin, phone).
  69.    cin >> phone;
  70.  
  71.    // cout << phone invokes operator<< function by
  72.    // issuing the call operator<<(cout, phone).
  73.    cout << "The phone number entered was:" << endl
  74.         << phone << endl;
  75.    return 0;
  76. }
  77.  
  78.  
  79. *******end source
  80.  
  81. Here is the execution:
  82.  
  83. % ./fig8.3
  84. Enter a phone number in the form (123) 456-7890:
  85. (800) 555-1212
  86. The phone number entered was:
  87. (800) -
  88. %
  89.  
  90. The output should be the full number.  
  91.  
  92. I tested the value of the PhoneNumber object with
  93. printExchange() and printLine() functions.  Both print
  94. funtions printed nothing leading me to conclude that the
  95. culprit is the function:
  96.  
  97. friend istream &operator>>(istream &, PhoneNumber &);
  98.  
  99.  
  100. I am stumped!  Is there a switch I need to throw to make
  101. operator overloading work with g++?  A search of the man pages
  102. has turned up nothing.
  103.  
  104.  
  105. Thnaks for any light you can shed!
  106.  
  107. Best Regards,
  108.  
  109. Arthur
  110. acinader@panix.com
  111.